
/**
 * productType 商品类型，0：全部； 1：时尚； 2：美妆； 3：母婴； 4：书籍； 5：居家； 6：数码； -1：其他
 */
function requestProducts(param, callback) {
  if (typeof productType == "function") {
    callback = productType
    productType = 0
  }

  getApp().func.Get(false, getApp().api.goods_list, param,
    function (data) {
      typeof callback == "function" && callback(data)
    },
    function (error) {
      wx.showToast({
        title: error.data.message,
        icon: 'none',
        duration: 2000,
      })
    }
  )
};

//获取可购买商品列表
function goodsCanBy(page, callback) {
  let data = {
    'startPage': page,
    'userId': getApp().globalData.userId,
  }
  getApp().func.Get(true, getApp().api.goods_can_buy, data, function (res) {
    typeof callback == "function" && callback(res)
  })
};
//获取还有发布的商品列表
function friendsGoods(page, callback) {
  let data = {
    'startPage': page,
    'type': 0,
    'sort': 0,
  }
  getApp().func.Get(true, getApp().api.goods_friend_put, data, function (res) {
    typeof callback == "function" && callback(res)
  })
};

/**
 * 用户扫码渠道统计
 */
function inserUserChannel(sence){
  getApp().func.Post(true, getApp().api.inserUserChannel,
    { "channel": sence},
    function (data) {
    },
    function (error) {
    })
}
/**
 *  模板消息发送
 *  
 */
function sendMessage(param, success, fail) {
  getApp().func.Get(false, getApp().api.share_setting, param,
    function (data) {
      typeof success == "function" && success(data)
    },
    function (error) {
      typeof fail == "function" && fail(error)
    })
}
/**
 *  分享信息获取
 *  type分享类型  1 首页，2 任务、签到 3、拼团列表分享 4 拼团详情页， 5邀新拼团分享 6 商品详情分享 7 砍价分享，8 发布的商品分享 9 送出的订单分享，10买到的订单分享 15开心果冻引导页 16抢兑
 */
function shareSettings(type, success, fail) {
  getApp().func.Get(false, getApp().api.share_setting, { "type": type },
    function (data) {
      typeof success == "function" && success(data)
    },
    function (error) {
      typeof fail == "function" && fail(error)
    })
}

/**
 *  文案信息获取
 *  type分享类型  
 * 0 趣闲介绍 1 砍果冻规则 2 拼团倒计时 3 订单支付运费倒计时 4 拼团玩法简介1 5 拼团规则详情 6 签到活动规则 7 签到活动小果冻介绍 8 用户等级详情 9 发布指南 10 抢兑规则 11 拼团玩法简介2 12 拼团玩法简介3 13 发布商品赠送果冻数量 14 签到规则 15 抢兑相关
 */
function getSetting(settingType, success, fail) {
  getApp().func.Get(false, getApp().api.get_setting, { "type": settingType },
    function (data) {
      typeof success == "function" && success(data)
    },
    function (error) {
      typeof fail == "function" && fail(error)
    })
}


function rewardRequest(goodsId, rewardIntegral, suc, fail) {
  getApp().func.Get(false, getApp().api.praise_candy, { "userId": getApp().globalData.userId, "goodsId": goodsId, "rewardIntegral": rewardIntegral },
    function (data) {
      typeof suc == "function" && suc(data)
    },
    function (error) {
      typeof fail == "function" && fail(error)
    })
}

function favRequest(relateId, goodsType, suc, fail) {
  getApp().func.Post(true, getApp().api.user_fav,
    { "userId": getApp().globalData.userId, "relateId": relateId, "type": goodsType },
    function (data) {
      if (data.code == 0) {
        typeof suc == "function" && suc(data)
      } else {
        typeof fail == "function" && fail(data.data)
      }
    },
    function (error) {
      typeof fail == "function" && fail(error.data)
    })
}

function deleteFavRequest(relateId, suc, fail) {
  getApp().func.Post(false, getApp().api.user_fav_del,
    { "userId": getApp().globalData.userId, "ralateId": relateId, "type": 1 },
    function (data) {
      if (data.code == 0) {
        typeof suc == "function" && suc(data)
      } else {
        typeof fail == "function" && fail(data.data)
      }
    },
    function (error) {
      typeof fail == "function" && fail(error.data)
    })
}


function likeRequest(relateId, suc, fail) {
  getApp().func.Post(false, getApp().api.user_like,
    { "userId": getApp().globalData.userId, "relateId": relateId, flag: "0" },
    function (data) {
      if (data.code == 200) {
        typeof suc == "function" && suc(data)
      } else {
        typeof fail == "function" && fail(data.data)
      }
    },
    function (error) {
      typeof fail == "function" && fail(error.data)
    })
}

function possilbeFriendsRequest(suc, fail) {
  getApp().func.Get(true, getApp().api.user_follower,
    { "userId": getApp().globalData.userId},
    function (data) {
      if (data != null) {
        typeof suc == "function" && suc(data)
      }
    },
    function (error) {
      typeof fail == "function" && fail(error)
    })
}

function addPossibleFriends(foucesUserId, suc, fail) {
  getApp().func.Post(false, getApp().api.put_fans,
    { "foucesUserId": foucesUserId, "userId": getApp().globalData.userId, "flag": 0 },
    function (data) {
      if (data.code == 200) {
        typeof suc == "function" && suc(data)
      } else {
        typeof fail == "function" && fail("关注失败")
      }
    },
    function (error) {
      typeof fail == "function" && fail(error)
    })
}

/**
 * 处理轮播图数据
 */
function setSwiperData(url) {
  let imageUrl = ''
  let index = url.lastIndexOf('/')
  let startUrl = url.substring(0, index) + '/zoom/'

  let str = url.substring(index + 1, url.length);
  let strIndex = str.lastIndexOf('.')
  let endUrl = str.substring(0, strIndex) + '.jpg'

  return startUrl + endUrl
}

/**
 * 处理封面
 */
function configCover(str) {
  if (str != null) {
    if (str.indexOf('zoom') == -1) {
      console.log('处理封面')
      var cover = str.split("").reverse().join("")
      var index = cover.indexOf('/') + 1
      cover = cover.slice(0, index) + 'mooz/' + cover.slice(index)
      cover = cover.split("").reverse().join("")
      return cover
    }else {
      return str
    }
  }
}

/**
 * 地址分割
 */
// function configAddress(res, callback) {    
//   var regex = /^(北京市|天津市|重庆市|上海市|香港特别行政区|澳门特别行政区)/;
//   var REGION_PROVINCE=[];
//   var addressBean = {
//     REGION_PROVINCE:null,
//     REGION_COUNTRY:null,
//     REGION_CITY:null,
//     ADDRESS:null};
//   function regexAddressBean(address, addressBean){
//     regex = /^(.*?[市州]|.*?地区|.*?特别行政区)(.*?[市区县])(.*?)$/g;
//       var addxress = regex.exec(address);
//       addressBean.REGION_CITY=addxress[1];
//       addressBean.REGION_COUNTRY=addxress[2];
//       addressBean.ADDRESS=addxress[3]+"("+res.name+")";
//       console.log(addxress);
//   }
//   if(!(REGION_PROVINCE = regex.exec(res.address))){
//     regex = /^(.*?(省|自治区))(.*?)$/;
//     REGION_PROVINCE = regex.exec(res.address);
//     addressBean.REGION_PROVINCE= REGION_PROVINCE[1];
//     regexAddressBean(REGION_PROVINCE[3],addressBean);
//   } else {
//     addressBean.REGION_PROVINCE= REGION_PROVINCE[1];
//     regexAddressBean(res.address, addressBean);
//   }
//   var data = { "province": addressBean.REGION_PROVINCE + " ", "city": addressBean.REGION_CITY + " ", "area": addressBean.REGION_COUNTRY}
//   typeof callback == "function" && callback(data)
// }

//获取更多商品
function getMoreListData(param, suc, fail) {
  getApp().func.Get(false, getApp().api.goods_more_list, param,
    function (data) {
      typeof suc == "function" && suc(data)
    },
    function (error) {
      typeof fail == "function" && fail(error)
    })
}

//获取红包详情
function getRedPacketDetail(suc, fail) {
  getApp().func.Post(true, getApp().api.groupbuy_packetdetail,
    function (data) {
      if (data != '') {
        typeof suc == "function" && suc(data)
      }else {
        typeof fail == "function" && fail('')
      }
      
    },
    function (error) {
      typeof fail == "function" && fail(error)
    })
}

// 消息总数量
function requestMessage(suc, fail) {
  getApp().func.Get(false, getApp().api.message_num,
    function (data) {
      if (data.unReadMsgNum && data.unReadMsgNum > 0) {
        var messageCount = data.unReadMsgNum > 99 ? '99+' : data.unReadMsgNum + ''
        wx.setTabBarBadge({
          index: 3,
          text: messageCount
        })
      } else {
        wx.removeTabBarBadge({
          index: 3,
        })
      }
      typeof suc == "function" && suc(messageCount)
    },
    function (error) {
      typeof fail == "function" && fail(error)
    })
}


module.exports = {
  requestMessage: requestMessage,
  favRequest: favRequest,
  deleteFavRequest: deleteFavRequest,
  likeRequest: likeRequest,
  possilbeFriendsRequest: possilbeFriendsRequest,
  addPossibleFriends: addPossibleFriends,
  shareSettings: shareSettings,
  setSwiperData: setSwiperData,
  getSetting: getSetting,
  configCover: configCover,
  inserUserChannel: inserUserChannel,
  getMoreListData: getMoreListData,
  getRedPacketDetail: getRedPacketDetail,
  requestProducts: requestProducts,
  goodsCanBy: goodsCanBy,
  friendsGoods: friendsGoods
};